<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="filters">
				<el-form-item>
					<el-input v-model="filters.keyword" placeholder="字典名称"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="queryListData">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="success" @click="handleUp">批量上架</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="danger" @click="handleDown">批量下架</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
		<el-table :data="depts"  highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
			<el-table-column type="selection" width="55">
			</el-table-column>
			<el-table-column type="index" width="60">
			</el-table-column>
			<el-table-column prop="name" label="昵称" >
			</el-table-column>
			<el-table-column prop="saleprice" label="售价" >
			</el-table-column>
			<el-table-column prop="offsaletime" label="下架时间" >
			</el-table-column>
			<el-table-column prop="onsaletime" label="上架时间" >
			</el-table-column>
			<el-table-column prop="costprice" label="成本价" >
			</el-table-column>
			<el-table-column prop="shop.name" label="店铺名" >
			</el-table-column>
			<el-table-column prop="type_id" label="品种" >
			</el-table-column>
			<el-table-column prop="state" label="状态" >
				<template scope="scope">
					<span style="color: #11b95c" v-if="scope.row.state===1">上架</span>
					<span style="color: red" v-else-if="scope.row.state===0">下架</span>
					<span style="color: plum" v-else>未知</span>
				</template>
			</el-table-column>
			<el-table-column label="操作" width="150">
				<template scope="scope" >
					<el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
					<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
				</template>
			</el-table-column>



		</el-table>

		<!--工具条-->
		<el-col :span="24" class="toolbar">
			<el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
			<el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="pageSize" :total="total" style="float:right;">
			</el-pagination>
		</el-col>

		<!--操作界面-->
		<el-dialog :title="title" :visible.sync="editFormVisible" :close-on-click-modal="false">
			<el-form :model="addForm" label-width="80px" :rules="addFormRules" ref="addForm">
				<el-form-item label="宠物名称" prop="name">
					<el-input v-model="addForm.name" ></el-input>
				</el-form-item>
				<el-form-item label="售价" prop="name">
					<el-input v-model="addForm.saleprice" ></el-input>
				</el-form-item>
				<el-form-item label="成本价" prop="name">
					<el-input v-model="addForm.costprice" ></el-input>
				</el-form-item>
				<el-form-item label="店铺选择">
					<el-select v-model="addForm.shop" value-key="id" clearable placeholder="请选择">
						<el-option
								v-for="item in shops"
								:key="item.id"
								:label="item.name"
								:value="item">
							<span style="float: left; color:#ff00ff; font-size: 13px">{{ item.name }}</span>
						</el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="支付方式" prop="paytype">
					<el-select v-model="value" placeholder="请选择">
						<el-option
								v-for="item in paytypes"
								:key="item.value"
								:label="item.label"
								:value="item.value">
						</el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="产品logo" prop="resources">
					<el-upload
							class="upload-demo"
							action="/api/dfs/upload"
							:on-preview="handlePreview"
							:on-remove="handleRemove"
							:on-success="handleSuccess"
							:file-list="fileList"
							list-type="picture">
						<el-button size="small" type="primary" >点击上传</el-button>
						<div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
					</el-upload>
				</el-form-item>
				<el-form-item label="状态">
					<el-radio-group v-model="addForm.state">
						<el-radio class="radio" :label="1">上架</el-radio>
						<el-radio class="radio" :label="0">下架</el-radio>
					</el-radio-group>
				</el-form-item>
				<el-form-item label="预约须知" prop="adoptNotice">
					<quill-editor v-model="addForm.detail.adoptNotice"
								  :options="quillOption">
					</quill-editor>
				</el-form-item>
				<el-form-item label="简介" prop="intro">
					<quill-editor v-model="addForm.detail.intro"
								  :options="quillOption">
					</quill-editor>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="editFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="addSubmit" :loading="addLoading">提交</el-button>
			</div>
		</el-dialog>
	</section>
</template>

<script>
	/*引入vue-quill-editor*/
    import { quillEditor } from 'vue-quill-editor'
    import 'quill/dist/quill.core.css'
    import 'quill/dist/quill.snow.css'
    import 'quill/dist/quill.bubble.css'

	import quillConfig from  "../../common/js/quill-config"
	export default {
        components: {
            quillEditor
        },
		data() {
			return {
                quillOption:quillConfig,
                paytypes:[{
                    value: '1',
                    label: '支付宝'
                }, {
                    value: '2',
                    label: '微信'
                }, {
                    value: '3',
                    label: '现金'
                }],
                value: '',
                fileList:[],
                title:'新增',// 默认模态框标题为新增
                depts: [],// 列表内容
				total: 0,// 当前页
				page: 1,
                shops:[],// 店铺选择
                pageSize:5,// 每页条数
                currentPage:1,// 默认第一页
				listLoading: false,
				sels: [],//列表选中列
                editFormVisible:false,
				editLoading: false,
				editFormRules: {
					name: [
						{ required: true, message: '请输入姓名', trigger: 'blur' }
					]
				},
				//编辑界面数据
                addForm:{//双向绑定的对象
                    id:'',
                    name:'',
					resources:'',
					saleprice:'',
					state:1,
					costprice:'',
					shop:null,
                    detail:{
                        intro:'',
                        adoptNotice:'',
					}
                },
                filters: {
                    keyword: ''
                },
				addLoading: false,
				addFormRules: {
					name: [
						{ required: true, message: '请输入姓名', trigger: 'blur' }
					]
				},
			}
		},
		methods: {
            getShopTrees(){
                this.$http.get("/shop").then(res=>{
                    this.shops=res.data;
                })
			},
		    /*分页*/
			handleCurrentChange(page) {

                this.currentPage=page;
				this.loadListData();
			},
			//获取用户列表
			loadListData() {
				let para = {
                    currentPage: this.currentPage,
                    pageSize:5,
					keyword: this.filters.keyword,
				}
				this.$http.post("/pet",para).then(res=>{
                    /*res是返回的数据*/
                    this.depts=res.data.bags;
				    this.total=res.data.total;
				})
			},
			/*高级查询*/
            queryListData(){
                currentPage: this.currentPage=1;
                this.loadListData();
			},
			//删除
			handleDel(i, row) {
                this.$confirm('亲，确认删除数据?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(()=>{
                let id = row.id;
                this.$http.delete("/pet/"+id).then(res=>{
                    if(res.data.success){
                        this.$message({
                            type:'success',
                            message:res.data.message
						});
					}else {
                        this.$message({
                            type: 'error',
                            message: res.data.message
                        });
					}
					// 刷新列表
					this.loadListData();
				})
                }).catch(()=>{
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                })

			},
			//显示编辑界面
			handleEdit(i, row) {
                this.fileList = [];
			    /*设置表标题*/
            	this.title='编辑';
            	/*复制的时候，有空属性会报错，这里要提前设置*/
                row.detail={};
                /*设置回显*/
                this.addForm=Object.assign({}, row);
                this.$http.get("/pet/detail/"+row.id)
                    .then(result=>{
                        this.addForm.detail=result.data;
                    })
                    .catch(result=>{
                        this.$message({
                            message: '系统错误',
                            type: 'error'
                        });
                    });
                let resourcesTmp = row.resources;//  /group1/M00/00/83/rBEAA2Bq0BSAbF1uAADyXoRe0ps940.jpg,/group1/M00/00/83/rBEAA2Bq0BqAT5kPAADy2SHkIGA358.jpg
				resourcesTmp = resourcesTmp.split(",");//[/group1/M00/00/83/rBEAA2Bq0BSAbF1uAADyXoRe0ps940.jpg,/group1/M00/00/83/rBEAA2Bq0BqAT5kPAADy2SHkIGA358.jpg]
                if (resourcesTmp!=null) {
                    for (let i = 0; i < resourcesTmp.length; i++) {
                        //因为在操作图片的时候，需要重新从fileList中取值赋值给表单
                        //fileList[i].response.resultObj;
                        this.fileList.push(
                            {
                                "url": "http://115.159.217.249:8888/" + resourcesTmp[i],
                                "response":
                                    {"resultObj": resourcesTmp[i]}
                            });
                    }
                }
                /*显示模态框*/
                this.editFormVisible = true;
			},
			//显示新增界面
			handleAdd() {
                /*设置表标题*/
                this.title='新增';
                this.fileList=[];
                /*清空列表*/
                this.addForm={//双向绑定的对象
                    id:'',
                    name:'',
                    resources:'',
                    saleprice:'',
                    state:1,
                    costprice:'',
                    detail:{
                        intro:'',
                        adoptNotice:'',
                    }
                };
                /*显示模态框*/
                this.editFormVisible = true;
			},
			//新增编辑
			addSubmit(){
			    /*检验表单规则*/
			    this.$refs.addForm.validate((valid)=>{
			        if(valid){// 符合规则
			            this.$confirm('确认提交吗？','提示',{}).then(()=>{
			                /*加载效果开启*/
			                this.addLoading = true;
			                /*拷贝对象*/
                            let para=Object.assign({}, this.addForm);
                            console.debug(para)
                            this.$http.put("/pet",para).then(res=>{
                                if(res.data.success){
                                    this.$message({
                                        type: 'success',
                                        message: res.data.message
                                    });
                                    //刷新数据
                                    this.loadListData();
                                }else {
                                    this.$message({
                                        type: 'error',
                                        message: res.data.message
                                    });
                                }
                            })
                            //关闭加载效果
                            this.addLoading=false;
                            //关闭模态框
                            this.editFormVisible=false;
						})
					}
				})
			},
			selsChange(sels) {
				this.sels = sels;
			},
			//批量删除
			batchRemove() {
			    /*获取选中的id*/
                var ids = this.sels.map(item => item.id);
				/*删除警告*/
				this.$confirm('确认删除选中记录吗？', '提示', {
					pro: 'warning'
				}).then(() => {
				    /*打开警告框*/
					this.listLoading = true;
					this.$http.patch("/pet",ids).then(res=>{
                        this.listLoading = false;
                        if(res.data.success){
                            this.$message({
                                message: '删除成功',
                                type: 'success'
                            });
                        }else{
                            this.$message({
                                message: res.data.msg,
                                type: 'error'
                            });
                        }
					})
						this.loadListData();
				})
			},
			/*批量上架*/
            handleUp() {
                /*获取选中的id*/
                var ids = this.sels.map(item => item.id);
                /*删除警告*/
                this.$confirm('确认上架选中记录吗？', '提示', {
                    pro: 'warning'
                }).then(() => {
                    /*打开警告框*/
                    this.listLoading = true;
                    this.$http.patch("/pet/onsale",ids).then(res=>{
                        this.listLoading = false;
                        if(res.data.success){
                            this.$message({
                                message: '上架成功',
                                type: 'success'
                            });
                        }else{
                            this.$message({
                                message: res.data.msg,
                                type: 'error'
                            });
                        }
                    })
                    this.loadListData();
                    alert(1);
                })
            },
			/*批量下架*/
            handleDown() {
                /*获取选中的id*/
                var ids = this.sels.map(item => item.id);
                /*删除警告*/
                this.$confirm('确认下架选中记录吗？', '提示', {
                    pro: 'warning'
                }).then(() => {
                    /*打开警告框*/
                    this.listLoading = true;
                    this.$http.patch("/pet/offsale",ids).then(res=>{
                        this.listLoading = false;
                        if(res.data.success){
                            this.$message({
                                message: '下架成功',
                                type: 'success'
                            });
                        }else{
                            this.$message({
                                message: res.data.msg,
                                type: 'error'
                            });
                        }
                    })
                    this.loadListData();
                    alert(1);
                })
            },
            /*图片上传功能实现*/
            handleSuccess(response, file, fileList){
                let resourceTemp = "";
                for(let i=0;i<fileList.length;i++){
                    //如果是数组的最后一个元素，不要加逗号了
                    if(i == fileList.length -1){
                        resourceTemp = resourceTemp + fileList[i].response.resultObj;
                    }else{//如果不是最后一个 拼接上逗号
                        resourceTemp = resourceTemp + fileList[i].response.resultObj + ",";
                    }
                }
                this.addForm.resources = resourceTemp;
            },
            handleRemove(file, fileList) {
                var filePath =file.response.resultObj;
                this.$http.delete("/dfs/?path="+filePath)
                    .then(res=>{
                        if(res.data.success){
                            this.$message({
                                message: '删除成功!',
                                type: 'success'
                            });
                            if(resourceTemp) {
                                let resourceTemp = "";
                                for (let i = 0; i < fileList.length; i++) {
                                    //如果是数组的最后一个元素，不要加逗号了
                                    if (i == fileList.length - 1) {
                                        resourceTemp = resourceTemp + fileList[i].response.resultObj;
                                    } else {//如果不是最后一个 拼接上逗号
                                        resourceTemp = resourceTemp + fileList[i].response.resultObj + ",";
                                    }
                                }
                            }
                            this.addForm.resources = resourceTemp;
                        }else{
                            this.$message({
                                message: '删除失败!',
                                type: 'error'
                            });
                        }
                    })
            },
            handlePreview(file) {
            },
		},
		mounted() {
			this.loadListData();
			this.getShopTrees();
		}
	}

</script>

<style scoped>

</style>